<script>
function getJSDateTime(d){
    // padding function
    var s = function(a,b){return(1e15+a+"").slice(-b)};

    // default date parameter
    if (typeof d === 'undefined'){
        d = new Date();
    };

    // return ISO datetime
    return s(d.getMonth()+1,2) + '/' +
    	s(d.getDate(),2) + '/' +
		d.getFullYear() + ' ' +
        s(d.getHours(),2) + ':' +
        s(d.getMinutes(),2);// + ':' +
        //s(d.getSeconds(),2);
}
function getISODateTime(d){
	    // padding function
	    var s = function(a,b){return(1e15+a+"").slice(-b)};

	    // default date parameter
	    if (typeof d === 'undefined'){
	        d = new Date();
	    };

	    // return ISO datetime
	    return s(d.getDate(),2) + '/' +
	    	s(d.getMonth()+1,2) + '/' +
    		d.getFullYear() + ' ' +
	        s(d.getHours(),2) + ':' +
	        s(d.getMinutes(),2);// + ':' +
	        //s(d.getSeconds(),2);
	}
</script>
<?php
/* @var $this CalendarController */

$this->breadcrumbs=array(
	'Calendar',
);
date_default_timezone_set('Europe/Paris');

include ("newEventStudent.php");
include ("editEventStudent.php");

?>


<script>
	

	$(document).ready(function() {
		 $('#calendar').fullCalendar({
			header: {
				left: 'prev,next today',
				center: 'title',
				right: 'month,agendaWeek,agendaDay'
			},
			titleFormat: {
		        month: 'MMMM yyyy',
		        week: "d [ MMMM][ yyyy] {'&#8212;' d MMMM yyyy}",
		        day: 'dddd'
		    },
			selectable: true,
			selectHelper: true,
			disableDragging: true,
			disableResizing: true,
			allDaySlot: false,
			
			
			eventClick: function(calEvent, jsEvent, view) {

				if($(this).children('.fc-event-inner').css("background-color")=="rgb(0, 170, 0)")
				{
					$("#edit-event-station").html('');			
	
					$('#edit-event-form .startdate').val(getISODateTime(calEvent.start));
					$('#edit-event-form .enddate').val(getISODateTime(calEvent.end));
				
					$('#edit-event-form .id').val(calEvent.id);
					$('#edit-event-form .stationname').val(calEvent.station);
					$('#edit-event-form .occupied').val(calEvent.zajete);
					$('#edit-event-form .limit').val(calEvent.limit);
					
					$("#edit-event-dlg" ).dialog("open");
					
					$.post(
					'<?php echo Yii::app()->request->baseUrl;?>/index.php/calendar/editStudentReservation',
					{
						id:calEvent.id
					},
					readStations,
					"json"
				);
				}
				else
				{
					$("#new-event-station").html('');			
	
					$('#new-event-form .startdate').val(getISODateTime(calEvent.start));
					$('#new-event-form .enddate').val(getISODateTime(calEvent.end));
				
					$('#new-event-form .id').val(calEvent.id);
					$('#new-event-form .stationname').val(calEvent.station);
					$('#new-event-form .occupied').val(calEvent.zajete);
					$('#new-event-form .limit').val(calEvent.limit);

					$("#new-event-dlg" ).dialog("open");
					
					$.post(
					'<?php echo Yii::app()->request->baseUrl;?>/index.php/calendar/editStudentReservation',
					{
						day : calEvent.start.getDate(),
							month : calEvent.start.getMonth()+1,
							year : calEvent.start.getFullYear(),
							hourStart:calEvent.start.getHours(),
							hourStop:calEvent.end.getHours(),
							minStart:calEvent.start.getMinutes(),
							minStop:calEvent.end.getMinutes(),
						id:calEvent.id
					},
					readStations,
					"json"
					);
				}
				

		    },
			eventDrop: function(event, delta) {
				//alert(event.title + ' was moved ' + delta + ' days\n' +
				//	'(should probably update your database)');
			},
			eventRender: function(event, element) {
				element.children('.fc-event-inner').append(
					'<div>Limit studentów: '+event.zajete+' / '+ event.limit+'</div>'
				);
				
				if(event.reserved!=0)
				{
					element.children('.fc-event-inner').css("background-color", "rgb(0, 170, 0)");
				}
				
		    },
			editable: true,
			events: '<?php echo Yii::app()->request->baseUrl;?>/index.php/calendar/events'
		});
		
	});

	

</script>
<h1>Harmonogram</h1>
<div id='calendar' class='calendar'></div>
